package com.cn.bukeManage.service.serviceImpl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cn.bukeManage.dao.ShoppingmallDao;
import com.cn.bukeManage.pojo.*;
import com.cn.bukeManage.pojo.page.Page;
import com.cn.bukeManage.pojo.page.SamplePage;
import com.cn.bukeManage.service.ShoppingmallService;
import com.cn.bukeManage.utils.ClothCurtain.Arith;
import com.cn.bukeManage.utils.upload.DeleteLocalFileUtils;
import com.cn.bukeManage.utils.upload.UploadUtils;
import com.google.gson.JsonObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("shoppingmallService")
@Transactional(rollbackFor=Exception.class)
public class ShoppingmallServiceImpl implements ShoppingmallService {
	@Resource
	private ShoppingmallDao shoppingmallDao;

	//通过状态得到所有的商品
	public List<SamplePage> getAllGoodsByPage(Model model,Integer currentPage,Integer state) {
		SamplePage page=new SamplePage();
		page.setCurrentPage(currentPage);
		page.setState(state);
		List<SamplePage> samples=shoppingmallDao.getAllGoodsByPage(page);	
		List<AccessoriesType> acc=shoppingmallDao.getAllAccTyp();
		List<FinishProductType> finType=shoppingmallDao.getAllFinType();
		List<Sample_style> styles=shoppingmallDao.getAllStyle();
		Integer rows=shoppingmallDao.getAllRowsOfSample(page);
		page.setRows(rows);
		model.addAttribute("acc", acc);
		model.addAttribute("finType", finType);
		model.addAttribute("styles", styles);
		model.addAttribute("page", page);
		return samples;

	}

	//閸掑棝銆夊妤�鍩岄幍锟介張澶屾畱濞茶濮�
	public List<Activity> getAllActivityByPage(Model model,Integer currentPage) {
		Page page=new Page();
		page.setCurrentPage(currentPage);
		List<Activity> activities=shoppingmallDao.getAllActivityByPage(page);
		Integer rows =shoppingmallDao.getAllRowsOfActivity();
		page.setRows(rows);
		model.addAttribute("page", page);
		return activities;
	}
	//閸掑棝銆夊妤�鍩岄幍锟介張澶屾畱妞嬪孩鐗�
	public List<Style> getAllStyleByPage(Model model, Integer currentPage) {
		Page page=new Page();
		page.setCurrentPage(currentPage);
		List<Style> styles=shoppingmallDao.getAllStyleByPage(page);
		Integer rows =shoppingmallDao.getAllRowsOfStyle();
		page.setRows(rows);
		model.addAttribute("page", page);		
		return styles;
	}
	//閸掑棝銆夐懢宄板絿閹碉拷閺堝娈戦柅鍌滄暏閸︾儤娅�
	public List<Location> getAllLocationByPage(Model model, Integer currentPage) {
		Page page=new Page();
		page.setCurrentPage(currentPage);
		List<Location> locations=shoppingmallDao.getAllLocationByPage(page);
		Integer rows =shoppingmallDao.getAllRowsOfLocation();
		page.setRows(rows);
		model.addAttribute("page", page);			
		return locations;
	}
	//閸掑棝銆夐懢宄板絿閹碉拷閺堝娈戝锟介柅姘隘閸╋拷
	public List<Territory> getAllTerritoriesByPage(Model model, Integer currentPage) {
		Page page=new Page();
		page.setCurrentPage(currentPage);
		List<Territory> territories=shoppingmallDao.getAllTerritoryByPage(page);
		Integer rows =shoppingmallDao.getAllRowsOfTerritory();
		page.setRows(rows);
		model.addAttribute("page", page);			
		return territories;
	}
	//閹碉拷閺堝〖ype
	public List<Map<String, Object>> getAllType() {
		
		List<FinishProductType> finType = shoppingmallDao.getAllFinType();
		List<AccessoriesType> acct = shoppingmallDao.getAllAccTyp();
		List<Map<String,Object>> list=new ArrayList<>();
		for(FinishProductType finishProductType:finType){
			Map<String,Object> map=new HashMap<>();
			map.put("id",finishProductType.getFptId());
			map.put("body",finishProductType.getFptBody());
			map.put("type", "閹存劕鎼х敮锟�");
			list.add(map);
		}
		for(AccessoriesType accessoriesType:acct){
			Map<String,Object> map=new HashMap<>();
			map.put("id",accessoriesType.getAccessoriesTypeId());
			map.put("body",accessoriesType.getAccessoriesTypeContent());
			map.put("type", "闁板秳娆�");
			list.add(map);
		}
		return list;
	}
	//通过商品类型得到商品分页信息
	public List<SamplePage> getAllGoodsByPageAndType(Model model, Integer currentPage, Integer type,Integer state) {
		SamplePage page=new SamplePage();
		page.setCurrentPage(currentPage);
		page.setType(type);
		page.setState(state);
		List<SamplePage> samples=shoppingmallDao.findGoodsbyType(page);
		List<AccessoriesType> acc=shoppingmallDao.getAllAccTyp();
		List<FinishProductType> finType=shoppingmallDao.getAllFinType();
		List<Sample_style> styles=shoppingmallDao.getAllStyle();
		Integer rows=shoppingmallDao.getAllRowsOfSampleByType(page);
		page.setRows(rows);
		model.addAttribute("acc", acc);
		model.addAttribute("finType", finType);
		model.addAttribute("styles", styles);
		model.addAttribute("page", page);
		return samples;		

	}
	//闁俺绻冨ú璇插Зid閼惧嘲褰囬幍锟介張澶嬫た閸旓拷
	public Activity getActivityById(Integer activityId) {
		Activity activity=shoppingmallDao.getActivityById(activityId);
		return activity;
	}
	//娣囶喗鏁煎ú璇插З
	public Integer updateActivity(Activity activity) {
		Integer integer=shoppingmallDao.updateActivity(activity);
		return integer;
	}
	//閺傛澘顤冨ú璇插З
	public Integer addActivity(Activity activity) {
		Integer integer=shoppingmallDao.addActivity(activity);
		return integer;
	}
	public Style getStyleById(Integer styleId) {
		Style style=shoppingmallDao.getStyleById(styleId);
		return style;
	}
	public Integer updateStyle(Style style) {
		Integer integer =shoppingmallDao.updateStyle(style);
		return integer;
	}
	
	public Integer addStyle(Style style) {
		Integer integer=shoppingmallDao.addStyle(style);
		return integer;
	}
	public Location getLocationById(Integer locationId) {
		Location location=shoppingmallDao.getLocationById(locationId);
		return location;
	}
	public Integer updateLocation(Location location) {
		Integer integer=shoppingmallDao.updateLocation(location);
		return integer;
	}
	public Integer addLocation(Location location) {
		Integer integer=shoppingmallDao.addLocation(location);
		return integer;
	}
	//閸掔娀娅庨柅鍌滄暏閸︾儤娅�
	public Integer deleteLocation(Integer locationId) {
		Integer date = shoppingmallDao.selectBLocation(locationId);
		if(date>0){
			return 0;
		}else{
			Integer integer = shoppingmallDao.deleteLocation(locationId);
			return integer;
		}
	}
	//閸掔娀娅庢搴㈢壐
	public Integer deleteStyle(Integer styleId) {
		Integer date = shoppingmallDao.selectBStyle(styleId);
		if(date>0){
			return 0;
		}else{
			Integer integer = shoppingmallDao.deleteStyle(styleId);
			return integer;
		}
	}
	//閸掔娀娅庡ú璇插З
	public Integer deleteActivity(Integer activityId) {
		Integer integer = shoppingmallDao.deleteActivity(activityId);
		return integer;
	}
	//娑撳﹣绱堕崶鍓у
	public String shangchuanTupian(MultipartFile fileList,HttpServletRequest request) {
		String path = "temp/img";
		// 鏉╂瑤閲滈弰顖氭躬閺堫剙婀撮張宥呭閸ｃ劋绗傞棃銏ｇ獓
		path = request.getSession().getServletContext().getRealPath(path);
		// 閺傚洣娆㈤惃鍕倳鐎涳拷
		String name = String.valueOf(System.currentTimeMillis()) + "_" + Math.round(Math.random() * 90000 + 10000) + ""
				+ fileList.getOriginalFilename();
		// 娑撳﹣绱堕崚鐗堟箛閸斺�虫珤閺堫剙婀�
		UploadUtils.uploadLocalTomcat(path, name, fileList);
		// 娑撳﹣绱堕崚棰佺閻楋拷
		int codeStatus = UploadUtils.uploadQiNiu(path, name);
		if (codeStatus == 200) {
			System.out.println("success");
			// 娑撳﹣绱堕崚棰佺閻楁稒鍨氶崝锟�!
			// 閸ュ墽澧栭崷鏉挎絻鐎涙ê鍩岄張顒�婀撮弫鐗堝祦鎼存挷鑵�

		} else {
			// 娑撳﹣绱堕崚棰佺閻楁稑銇戠拹锟�!
			
		}

		// 閸掔娀娅庨張顒�婀撮張宥呭閸ｃ劑鍣烽棃銏㈡畱
		DeleteLocalFileUtils.delAllFile(path);

		return name;
		
	}
	//濞ｈ濮為弽宄版惂
	public int insertSample(String str) {
		System.out.println(str);
		return 11;
	}
	//閼惧嘲褰囬崗銊╁劥妞嬪孩鐗�
	public List<Style> getAllStyle() {
		//閼惧嘲绶遍崗銊╁劥妞嬪孩鐗�
		List<Style> styles = shoppingmallDao.getAllStyleS();
		return styles;
	}
	//閼惧嘲褰囬崗銊╁劥鐎瑰顥婃担宥囩枂
	public List<Location> getAllLocation() {
		List<Location> locations = shoppingmallDao.getAllLocationS();
		return locations;
	}
	
	
	
	
	
	// 娣诲姞鍟嗗搧
	@Transactional
	public String addgoods(String sample, String supplier,String properties, String compositions, String assistImg, HttpServletRequest request) throws IOException {
		String code = "0";
		//鍒ゆ柇鍙傛暟鏄惁涓虹┖
		if(null==sample){
			return code;
		}
		Sample sampleObj = JSONObject.parseObject(sample, Sample.class);
		JSONArray com = JSONArray.parseArray(compositions);
		if (null != com) {
				for (int i = 0; i < com.size(); i++) {
					Composition c = JSONObject.parseObject(com.get(i).toString(), Composition.class);
					 
					if(c.getZhePrice()!=null){
						sampleObj.setZhePrice(c.getZhePrice());
					}
					if(c.getLlPrice()!=null){
						sampleObj.setLlPrice(c.getLlPrice());
					}
					if(c.getHolePrice()!=null){
						sampleObj.setHolePrice(c.getHolePrice());
					}
					if(c.getSsPrice()!=null){
						sampleObj.setSsPrice(c.getSsPrice());
					}					
			}
		}
		 
		if(sampleObj.getHolePrice()!=null&&sampleObj.getZhePrice()!=null){
			sampleObj.setIsSty(2);
		}else if(sampleObj.getHolePrice()!=null){
			sampleObj.setIsSty(0);
		}else if(sampleObj.getZhePrice()!=null){
			sampleObj.setIsSty(1);
		}
		
		
		if(sampleObj.getIsSty()!=null&&sampleObj.getSsPrice()!=null){
			sampleObj.setStructure(2);
		}else if(sampleObj.getSsPrice()!=null){
			sampleObj.setStructure(1);
		}else if(sampleObj.getIsSty()!=null){
			sampleObj.setStructure(0);
		}
		
		
		if( null!=sampleObj && null==sampleObj.getSeries() ){	
			//鏌ヨ鍟嗗搧绯诲垪鍙锋渶澶у彿鐮�
			Integer series =  shoppingmallDao.selectSeries()==null?1:shoppingmallDao.selectSeries();
			sampleObj.setSeries(series+1);
			sampleObj.setSeriesSample(1);
		}
		//鍟嗗搧鍘傚淇℃伅
		if(null!=supplier && !"".equals(supplier) ){
			Supplier supplier2 =new Supplier();
			supplier2.setName(supplier);
			shoppingmallDao.insertsupplier(supplier2);
			sampleObj.setSupplierId(supplier2.getSupplierId());
		}
		// 鍟嗗搧娣诲姞鍒版暟鎹簱
		
		shoppingmallDao.insertSample(sampleObj);
		
		
		JSONArray assistImgArr = JSONArray.parseArray(assistImg);
		List<Object>list=new ArrayList<Object>();
		for(Object a:assistImgArr){
			if(!list.contains(a)){
			list.add(a);
			}
		}
		 if(null!=list){
			 Integer graphicDetailsPX = 1;
			 for(Object a:list){
				 String d = a.toString();
				 Map<String, Object> map = new HashMap<>();
				 double pictureScale;
					try{
						  BufferedImage s = ShoppingmallServiceImpl.getBufferedImage("http://okwc9gyzs.bkt.clouddn.com/"+d);
						  System.out.println(Arith.div((double)s.getWidth(),(double)s.getHeight(),10));
						  pictureScale = Arith.div((double)s.getHeight(),(double)s.getWidth(),10);
						  }catch(IOException e){
							  throw new RuntimeException();
						  }
					map.put("sampleId", sampleObj.getSampleId());
					map.put("graphicDetails", d);
					map.put("graphicDetailsPX", graphicDetailsPX);
					map.put("pictureScale",pictureScale);
					shoppingmallDao.insertDetailsImg(map);
					graphicDetailsPX++;
			 }
		 }
	
		 
		/*if(null!=assistImgArr){
			Integer graphicDetailsPX = 1;
			for (int a = 0; a < assistImgArr.size(); a++) {
				Map<String, Object> map = new HashMap<>();
				String graphicDetails =assistImgArr.getString(a);
				 double pictureScale;
				try{
					  BufferedImage s = ShoppingmallServiceImpl.getBufferedImage("http://okwc9gyzs.bkt.clouddn.com/"+graphicDetails);
					  System.out.println(Arith.div((double)s.getWidth(),(double)s.getHeight(),10));
					  pictureScale = Arith.div((double)s.getWidth(),(double)s.getHeight(),10);
					  }catch(IOException e){
						  throw new RuntimeException();
					  }
				map.put("sampleId", sampleObj.getSampleId());
				map.put("graphicDetails", assistImgArr.getString(a));
				map.put("graphicDetailsPX", graphicDetailsPX);
				map.put("pictureScale",pictureScale);
				shoppingmallDao.insertDetailsImg(map);
				graphicDetailsPX++;
			}
		}*/
		// 灞炴�ч泦鍚�
		JSONObject propertiesObj = JSONObject.parseObject(properties);
		System.out.println("json"+propertiesObj);
		if (null != propertiesObj) {
			String styleStr = propertiesObj.getString("styleIds");
			System.out.println("str鐨勫��"+styleStr);
			String[] styleIds=null;
			if(styleStr !=null && !"".equals(styleStr)){
				
				styleIds = styleStr.substring(1, styleStr.length() - 1).split(",");
				
				if (styleIds != null && styleIds.length > 0) {
					for (int a = 0; a < styleIds.length; a++) {
						// 灏佽鍟嗗搧椋庢牸绫诲瀷鏁版嵁
						Map<String, Object> map = new HashMap<>();
						map.put("sampleId", sampleObj.getSampleId());
						map.put("styleId", styleIds[a]);
						shoppingmallDao.insertStyle(map);
						
					}

				}
			}	
			String locationsStr = propertiesObj.getString("locationIds");
			System.out.println("閫傜敤鍒涙櫙"+locationsStr);
			String[] locations=null;
			if(locationsStr!=null && !"".equals(locationsStr) ){
				locations = locationsStr.substring(1, locationsStr.length() - 1).split(",");
				// 閫傜敤鍦烘櫙
				for (String location : locations) {
					Map<String, Object> map = new HashMap<>();
					map.put("sampleId", sampleObj.getSampleId());
					map.put("locationId", location);
					shoppingmallDao.insertLocation(map);
				}
			}
		}

		// 闈㈡枡
		JSONArray compositionsArr = JSONArray.parseArray(compositions);
		System.out.println("闈㈡枡"+compositionsArr);
		if (null != compositionsArr) {
			for (int i = 0; i < compositionsArr.size(); i++) {
				Composition composition = JSONObject.parseObject(compositionsArr.getString(i), Composition.class); 
				//鍟嗗搧鍘傚淇℃伅
				if(null!=supplier && !"".equals(supplier) ){
					Supplier supplier2 =new Supplier();
					supplier2.setName(supplier);
					shoppingmallDao.insertsupplier(supplier2);
					composition.setSupplierId(supplier2.getSupplierId());
				}
				
				
				if(composition.getSpur()==null){
					composition.setSpur(0.0);
				}
				
				
				if(composition.getType()==3){
					composition.setPrice(composition.getSsPrice());
				}
				composition.setSampleId(sampleObj.getSampleId());
				shoppingmallDao.insert_compositions(composition);
			}
			code = "1";
		}
		return code;

	}
	
	
	/**
	 * 鏌ヨ鍟嗗搧淇″彿锛屽垽鏂槸鍚﹂噸澶�
	 * @return String   code  0 涓嶉噸澶�  1閲嶅
	 */
	@Override
	public String select_serialnumber(String serialnumber) {
		
		String  code="0";
		List<String> serialnumbers=shoppingmallDao.select_serialnumber();	
		for (String map : serialnumbers) {
			//鍒ゆ柇鍟嗗搧鍨嬪彿鏄惁瀛樺湪
			if(null!=map || !"".equals(map)){	
				if(serialnumber.toString().equals(map)){
					code="1";
					return code;
				}
			}
		}
		return code;
	}
	
	
	
	//閰嶄欢绫诲瀷
		@Override
		public List<AccessoriesType> getAllAccTyp() {
			// TODO Auto-generated method stub
			return shoppingmallDao.getAllAccTyp();
		}

		// 娣囨牴鎹畇ampleId鏌ヨ鍟嗗搧
		public Sample getSampleBySampleId(Map<String, Object> map,Model model ) {
			Sample sample = shoppingmallDao.getSampleBySampleId(map);
			model.addAttribute("fSProfit", sample.getFSProfit());
			List<AccessoriesType> acc = shoppingmallDao.getAllAccTyp();
			List<FinishProductType> finType = shoppingmallDao.getAllFinType();
			if(null!=sample.getSupplierId()){
				//渚涘簲鍟嗗悕绉�
				model.addAttribute("supplier", shoppingmallDao.selectsupplierbyid(sample.getSupplierId()));
			}else{
				model.addAttribute("supplier", "鏃�");
			}
			//閰嶄欢绫诲瀷
			model.addAttribute("acc", acc);
			//閰嶄欢绫诲瀷鎴愬搧甯樼被鍨�
			model.addAttribute("finType", finType);
			return sample;
		}

		/**
		 *鏍规嵁sampleId鏌ヨ闈㈡枡浣跨敤
		 */
		@Override
		public List<Composition> findCompositionBySampleId(Integer sampleId) {
			
			return shoppingmallDao.findCompositionBySampleId(sampleId);
		}

		/**
		 *鏍规嵁sampleId鏌ヨSample_style
		 */
		@Override
		public List<Sample_style> getAllsample_stylebysampleId(Integer sampleId) {
			return shoppingmallDao.getAllsample_stylebysampleId(sampleId);
		}

		/**
		 *鏍规嵁sampleId鏌ヨSample_location
		 */
		@Override
		public List<Sample_location> getAllsample_locationsampleId(Integer sampleId) {
			
			return shoppingmallDao.getAllsample_locationbysampleId(sampleId);			
		}
		//鎴愬搧甯樼被鍨�
		@Override
		public List<FinishProductType> getAllFinType() {
			
			return shoppingmallDao.getAllFinType();
		}
		/**
		 * 通过id查询商品信息
		 */
		public List<SamplePage> select_samplebyserialnumber(Model model, Integer serialnumber,Integer state) {
			Page page = new Page();
			page.setCurrentPage(1);
			SamplePage samplePage=new SamplePage();
			samplePage.setSampleId(serialnumber);
			samplePage.setCurrentPage(1);
			samplePage.setState(state);
			List<SamplePage> samples = shoppingmallDao.getAllGoodsByPageAndType(samplePage);
			List<AccessoriesType> acc = shoppingmallDao.getAllAccTyp();
			List<FinishProductType> finType = shoppingmallDao.getAllFinType();
			List<Sample_style> styles = shoppingmallDao.getAllStyle();
			page.setRows(1);
			model.addAttribute("acc", acc);
			model.addAttribute("finType", finType);
			model.addAttribute("styles", styles);
			model.addAttribute("page", page);
			return samples;
		}


		/**
		 * 鏍规嵁鍟嗗搧id淇敼鍟嗗搧
		 * @throws IOException 
		 */
		@Transactional
		public String updateSample(String sample ,String supplier, String properties, String compositions, String assist,
				HttpServletRequest request) throws IOException {
		
			String code = "0";
			if (null==sample) {
				
				return code;
			}	
			// sample
			
			Sample sampleObj = JSONObject.parseObject(sample, Sample.class);
			System.out.println("sampleObj"+":"+sampleObj);
			 JSONArray com = JSONArray.parseArray(compositions);
 
		if (null != com) {
					for (int i = 0; i < com.size(); i++) {
						Composition c = JSONObject.parseObject(com.get(i).toString(), Composition.class);
						 
						if(c.getZhePrice()!=null){
							sampleObj.setZhePrice(c.getZhePrice());
						}
						if(c.getLlPrice()!=null){
							sampleObj.setLlPrice(c.getLlPrice());
						}
						if(c.getHolePrice()!=null){
							sampleObj.setHolePrice(c.getHolePrice());
						}
						if(c.getSsPrice()!=null){
							sampleObj.setSsPrice(c.getSsPrice());
						}					

				}
			}
		if(sampleObj.getHolePrice()!=null&&sampleObj.getZhePrice()!=null){
			sampleObj.setIsSty(2);
		}else if(sampleObj.getHolePrice()!=null){
			sampleObj.setIsSty(0);
		}else if(sampleObj.getZhePrice()!=null){
			sampleObj.setIsSty(1);
		}
		
		
		if(sampleObj.getIsSty()!=null&&sampleObj.getSsPrice()!=null){
			sampleObj.setStructure(2);
		}else if(sampleObj.getSsPrice()!=null){
			sampleObj.setStructure(1);
		}else if(sampleObj.getIsSty()!=null){
			sampleObj.setStructure(0);
		}

			if (null==sampleObj.getSampleId()) {
				return code;
			}
			Integer sampleId=sampleObj.getSampleId();
			// 鍥剧墖
			JSONArray assistImgArr = JSONArray.parseArray(assist);
			List<Object>list=new ArrayList<Object>();
			for(Object a:assistImgArr){
				if(!list.contains(a)){
				list.add(a);
				}
			}
			if(null!=list){
				shoppingmallDao.deleteDetailsImg(sampleId);			
				Integer graphicDetailsPX = 1;
				for(Object i:list){
					String d=i.toString();
					 double pictureScale;
						try{
						  BufferedImage s = ShoppingmallServiceImpl.getBufferedImage("http://okwc9gyzs.bkt.clouddn.com/"+d);
						  System.out.println(Arith.div((double)s.getWidth(),(double)s.getHeight(),10));
						  pictureScale = Arith.div((double)s.getHeight(),(double)s.getWidth(),10);
						  }catch(IOException e){
							   throw new RuntimeException();
						  }
					Map<String, Object> map = new HashMap<>();
					map.put("sampleId", sampleObj.getSampleId());
					map.put("graphicDetails", d);
					map.put("graphicDetailsPX", graphicDetailsPX);
					map.put("pictureScale",pictureScale);
					shoppingmallDao.insertDetailsImg(map);
					graphicDetailsPX++;
				}
			}
			
			/*if(null!=assistImgArr){
				shoppingmallDao.deleteDetailsImg(sampleId);			
				Integer graphicDetailsPX = 1;
				for (int a = 0; a < assistImgArr.size(); a++) {
					String graphicDetails =assistImgArr.getString(a);
					 double pictureScale;
						try{
						  BufferedImage s = ShoppingmallServiceImpl.getBufferedImage("http://okwc9gyzs.bkt.clouddn.com/"+graphicDetails);
						  System.out.println(Arith.div((double)s.getWidth(),(double)s.getHeight(),10));
						  pictureScale = Arith.div((double)s.getWidth(),(double)s.getHeight(),10);
						  }catch(IOException e){
							   throw new RuntimeException();
						  }
					Map<String, Object> map = new HashMap<>();
					map.put("sampleId", sampleObj.getSampleId());
					map.put("graphicDetails", graphicDetails);
					map.put("graphicDetailsPX", graphicDetailsPX);
					map.put("pictureScale",pictureScale);
					shoppingmallDao.insertDetailsImg(map);
					graphicDetailsPX++;
				}		
			}*/
			if(null==sampleObj.getSeries() && null!=sampleObj){
				
				//鏌ヨ鍟嗗搧绯诲垪鍙锋渶澶у彿鐮�
				Integer sers=shoppingmallDao.selectSeries();
				Integer series =  sers==null?1:sers;
			
				sampleObj.setSeries(series+1);
				sampleObj.setSeriesSample(1);
			}
			//鍟嗗搧鍘傚淇℃伅
			if(null!=supplier && !"".equals(supplier ) ){
				if(null!=sampleObj.getSupplierId() && !"".equals(sampleObj.getSupplierId())){
				//鏍规嵁鍟嗗搧澶栭敭鏌ヨ渚涘簲鍟嗕俊鎭�
				Supplier spr=shoppingmallDao.selectsupplierbyid(sampleObj.getSupplierId());
				//鑻ヤ緵搴斿晢鏈変慨鏀�
				if(!supplier.equals(spr.getName())){
						shoppingmallDao.deletesupplierbyid(sampleObj.getSupplierId());
						Supplier supplier2 =new Supplier();
						supplier2.setName(supplier);
						shoppingmallDao.insertsupplier(supplier2);
						sampleObj.setSupplierId(supplier2.getSupplierId());
					}
				}else{
					//鑻ユ病鏈変緵搴斿晢
					Supplier supplier2 =new Supplier();
					supplier2.setName(supplier);
					shoppingmallDao.insertsupplier(supplier2);
					sampleObj.setSupplierId(supplier2.getSupplierId());
				}
			}
			// 鍟嗗搧娣诲姞鍒版暟鎹簱
			shoppingmallDao.updateSamplebyId(sampleObj);			// 灞炴�ч泦鍚�
			JSONObject propertiesObj = JSONObject.parseObject(properties);
			if (null != propertiesObj) {
				String styleStr = propertiesObj.getString("styleIds");
				
				String[] styleIds=null;
				if(styleStr !=null && !"".equals(styleStr)){
					styleIds = styleStr.substring(1, styleStr.length() - 1).split(",");
					if (styleIds != null && styleIds.length > 0) {
						//鍒犻櫎sample_style涓棿琛�
						shoppingmallDao.delete_sample_style(sampleId);
						for (int a = 0; a < styleIds.length; a++) {
							// 灏佽鍟嗗搧椋庢牸绫诲瀷鏁版嵁
							Map<String, Object> map = new HashMap<>();
							map.put("sampleId", sampleObj.getSampleId());
							map.put("styleId", styleIds[a]);
							shoppingmallDao.insertStyle(map);
						}

					}
				}	
				String locationsStr = propertiesObj.getString("locationIds");
				String[] locations=null;
				if(locationsStr!=null && !"".equals(locationsStr) ){
					locations = locationsStr.substring(1, locationsStr.length() - 1).split(",");
					
					shoppingmallDao.delete_sample_location(sampleId);
					for (String location : locations) {
						Map<String, Object> map = new HashMap<>();
						map.put("sampleId", sampleObj.getSampleId());
						map.put("locationId", location);
						shoppingmallDao.insertLocation(map);
					}
				}
			}
			// 闈㈡枡
			
			JSONArray compositionsArr = JSONArray.parseArray(compositions);
			
			if (null != compositionsArr||compositionsArr.size()!=0) {
				shoppingmallDao.delete_composition(sampleId);
				for (int i = 0; i < compositionsArr.size(); i++) {
					Composition composition = JSONObject.parseObject(compositionsArr.getString(i).toString(), Composition.class); 
					//鍟嗗搧鍘傚淇℃伅
					if(null!=supplier && !"".equals(supplier) ){
						Supplier supplier2 =new Supplier();
						supplier2.setName(supplier);
						shoppingmallDao.insertsupplier(supplier2);
						composition.setSupplierId(supplier2.getSupplierId());
					}
					
					if(composition.getSpur()==null){
						composition.setSpur(0.0);
					}
					
					
					if(composition.getType()==3){
						composition.setPrice(composition.getSsPrice());
					}
					composition.setSampleId(sampleObj.getSampleId());
					shoppingmallDao.insert_compositions(composition);
				}
				code = "1";
			}
			
			return code;		
		}

		@Override
		public List<Picture> findPicBySampleId(Integer sampleId) {
			List<Picture> picture = shoppingmallDao.findPicBySampleId(sampleId);
			
			return picture;
		}

		@Override
		public Sample selectSampleBySampleId(Integer SampleId) {
			return shoppingmallDao.selectSampleBySampleId(SampleId);
		}

		@Override
		public void updateGoodsByStateAndSampleId(Integer sampleId,Integer state) {
			
			shoppingmallDao.updateGoodsByStateAndSampleId(sampleId, state);
		}

		@Override
		public List<SamplePage> getGoodsByPage(Model model, Integer currentPage) {
			SamplePage page=new SamplePage();
			page.setCurrentPage(currentPage);
			List<SamplePage> samples=shoppingmallDao.getGoodsByPage(page);	
			List<AccessoriesType> acc=shoppingmallDao.getAllAccTyp();
			List<FinishProductType> finType=shoppingmallDao.getAllFinType();
			List<Sample_style> styles=shoppingmallDao.getAllStyle();
			Integer rows=shoppingmallDao.getAllRowsOfSample(page);
			page.setRows(rows);
			model.addAttribute("acc", acc);
			model.addAttribute("finType", finType);
			model.addAttribute("styles", styles);
			model.addAttribute("page", page);
			return samples;
		}
		public List<SamplePage> getGoodsByPageAndType(Model model, Integer currentPage, Integer type){
			SamplePage page=new SamplePage();
			page.setCurrentPage(currentPage);
			page.setType(type);
			List<SamplePage> samples=shoppingmallDao.findAllGoodsbyType(page);
			List<AccessoriesType> acc=shoppingmallDao.getAllAccTyp();
			List<FinishProductType> finType=shoppingmallDao.getAllFinType();
			List<Sample_style> styles=shoppingmallDao.getAllStyle();
			Integer rows=shoppingmallDao.getRowsOfSampleByType(page);
			page.setRows(rows);
			model.addAttribute("acc", acc);
			model.addAttribute("finType", finType);
			model.addAttribute("styles", styles);
			model.addAttribute("page", page);
			return samples;		

		}
		
		
		public List<SamplePage> select_samplebyId(Model model, Integer serialnumber) {
			Page page = new Page();
			page.setCurrentPage(1);
			SamplePage samplePage=new SamplePage();
			//samplePage.setSampleId(serialnumber);
			samplePage.setSerialnumber(serialnumber.toString());
			samplePage.setCurrentPage(1);
			List<SamplePage> samples = shoppingmallDao.getGoodsByPageAndType(samplePage);
			List<AccessoriesType> acc = shoppingmallDao.getAllAccTyp();
			List<FinishProductType> finType = shoppingmallDao.getAllFinType();
			List<Sample_style> styles = shoppingmallDao.getAllStyle();
			page.setRows(1);
			model.addAttribute("acc", acc);
			model.addAttribute("finType", finType);
			model.addAttribute("styles", styles);
			model.addAttribute("page", page);
			return samples;
		}

		@Override
		@Transactional(rollbackFor=Exception.class)
		public void deleteGoods(Integer SampleId) {
			if(SampleId!=null){
				shoppingmallDao.deleteSampleById(SampleId);
				shoppingmallDao.delete_sample_location(SampleId);
				shoppingmallDao.delete_sample_style(SampleId);
				shoppingmallDao.deleteCompositionById(SampleId);
				shoppingmallDao.deletePictureById(SampleId);
			}
		}

		@Override
		public void insert_compositions(String composition){
			if(composition!=null){
				Composition com = JSONObject.parseObject(composition,Composition.class);
				Sample sam=new Sample();
				sam.setSampleId(com.getSampleId());
				Sample u = shoppingmallDao.selectSampleBySampleId(com.getSampleId());
				
				if(u.getIsSty()!=null&&u.getSsPrice()!=null){
					sam.setStructure(2);
					
				}else if(u.getSsPrice()!=null){
					sam.setStructure(1);
				}else if(u.getIsSty()!=null){
					sam.setStructure(0);
				}
				
				if(com.getZhePrice()!=null){
					sam.setZhePrice(com.getZhePrice());
				}
				if(com.getLlPrice()!=null){
					sam.setLlPrice(com.getLlPrice());
				}
				if(com.getHolePrice()!=null){
					sam.setHolePrice(com.getHolePrice());
				}
				if(com.getSsPrice()!=null){
					sam.setSsPrice(com.getSsPrice());
				}					
				shoppingmallDao.updateSamplebyId(sam);
				shoppingmallDao.insert_compositions(com);
			}
			
		}

		@Override
		public void deleteCompositionByType(Integer sampleId, Integer type) {
			shoppingmallDao.deleteCompositionByType(sampleId, type);
		}
		
		
		 public static BufferedImage getBufferedImage(String imgUrl) throws IOException {
				BufferedImage img=null;
				try {
					URL url=new URL(imgUrl);
					InputStream in = url.openStream();
					 img = ImageIO.read(in);  
					} catch (MalformedURLException e) {
						e.printStackTrace();
					}
				return img;
			}
		
	}

